package com.screen.huiyue;

/**
 * Created by Kornan on 2018/4/18.
 */

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import java.io.File;


/**
 * Created by Kornan on 2018/4/16.
 */
public class Share2Dialog extends DialogFragment implements View.OnClickListener {

    private final static String TAG_AD = "tag_ad";

    public static Share2Dialog newInstance(String url) {
        Share2Dialog adDialog = new Share2Dialog();
        Bundle bundle = new Bundle();
        bundle.putString(TAG_AD, url);
        adDialog.setArguments(bundle);
        return adDialog;
    }

    public Share2Dialog() {

    }

    private ImageView ivClear;
    private ImageView ivContent;

    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow();
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.CENTER;
        params.width = (int) (dm.widthPixels * 0.8);
        params.height = (int) (dm.heightPixels * 0.8);
        win.setAttributes(params);
        setCancelable(false);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.dialog_share, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        ivClear = view.findViewById(R.id.ivClear);
        ivContent = view.findViewById(R.id.ivContent);

        try {
            if (getArguments() != null) {
                String url = getArguments().getString(TAG_AD);
                if (!TextUtils.isEmpty(url)) {
                    File imageFile = new File(url);
                    if (imageFile.exists()) {
                        ivContent.setImageBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()));
                    }
                }
            } else {
                dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        ivClear.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        dismiss();
    }
}